# review_page.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class ReviewPage:
    def __init__(self, driver):
        self.driver = driver
        self.url = 'http://example.com/reviews'
        self.product_name_locator = (By.ID, 'product-name')  # 商品名称定位器
        self.rating_stars_locator = (By.ID, 'rating-stars')  # 星级评分定位器
        self.submit_review_button_locator = (By.ID, 'submit-review-button')  # 提交评价按钮定位器
        self.review_text_area_locator = (By.ID, 'review-text-area')  # 评价文本输入框定位器
        self.review_success_message_locator = (By.ID, 'review-success-message')  # 评价成功消息定位器
        self.review_error_message_locator = (By.ID, 'review-error-message')  # 评价错误消息定位器
        self.reviews_list_locator = (By.ID, 'reviews-list')  # 评价列表定位器
        self.review_details_locator = (By.ID, 'review-details')  # 评价详情定位器
        self.average_rating_locator = (By.ID, 'average-rating')  # 平均评分定位器
        self.back_button_locator = (By.ID, 'back-button')  # 返回按钮定位器
        self.delete_review_button_locator = (By.ID, 'delete-review-button')  # 删除评价按钮定位器
        self.confirm_delete_button_locator = (By.ID, 'confirm-delete-button')  # 确认删除按钮定位器
        self.no_reviews_message_locator = (By.ID, 'no-reviews-message')  # 没有评价消息定位器

    # 导航到评价页面
    def navigate(self):
        self.driver.get(self.url)

    # 选择星级评分
    def select_rating(self, rating):
        self.driver.find_element(*self.rating_stars_locator).click()

    # 输入评价文本
    def enter_review_text(self, text):
        self.driver.find_element(*self.review_text_area_locator).send_keys(text)

    # 点击提交评价按钮
    def click_submit_review_button(self):
        self.driver.find_element(*self.submit_review_button_locator).click()

    # 获取评价成功消息
    def get_review_success_message(self):
        return self.driver.find_element(*self.review_success_message_locator).text

    # 获取评价错误消息
    def get_review_error_message(self):
        return self.driver.find_element(*self.review_error_message_locator).text

    # 获取评价列表
    def get_reviews_list(self):
        return self.driver.find_elements(*self.reviews_list_locator)

    # 获取评价详情
    def get_review_details(self):
        return self.driver.find_element(*self.review_details_locator).text

    # 获取平均评分
    def get_average_rating(self):
        return self.driver.find_element(*self.average_rating_locator).text

    # 点击返回按钮
    def click_back_button(self):
        self.driver.find_element(*self.back_button_locator).click()

    # 点击删除评价按钮
    def click_delete_review_button(self):
        self.driver.find_element(*self.delete_review_button_locator).click()

    # 确认删除评价
    def confirm_delete_review(self):
        self.driver.find_element(*self.confirm_delete_button_locator).click()

    # 获取没有评价的消息
    def get_no_reviews_message(self):
        return self.driver.find_element(*self.no_reviews_message_locator).text

    # 等待评价提交完成
    def wait_for_review_submission(self):
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.review_success_message_locator)
        )